﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using libProxy;
using libSkype;

namespace consoleSkypeProxy
{
    class Program
    {
        private static SkypeWrapper _wrapper;

        static void Main(string[] args)
        {
            var proxy = new HttpProxy();

            _wrapper = new SkypeWrapper();
            _wrapper.OnMessageReceived += MessageReceived;
            _wrapper.StartChat("vasiltsov.vladimir");

            proxy.OnReqestFromClient += ClientRequest;
            proxy.Start(IPAddress.Any, 9999);

            Console.WriteLine("Enter to exit");
            Console.ReadLine();
        }

        private static ManualResetEvent _waitSkypeResponse = new ManualResetEvent(false);
        private static string message;

        private static void ClientRequest(object sender, OnRequestFromClientEventArgs e)
        {
            _wrapper.SendMessage(e.Message);
            lock(_waitSkypeResponse)
            {
                if(_waitSkypeResponse.WaitOne())
                {
                    e.Message = message;
                }
            }
        }

        private static void MessageReceived(object sender, SkypeWrapperEventArgs e)
        {
            lock(_waitSkypeResponse)
            {
                _waitSkypeResponse.Set();
            }
            message = e.Message;
        }
    }
}
